package de.tum.in.eist.im.client;

import java.util.Date;
import java.util.List;

import de.tum.in.eist.im.client.domain.*;

import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.Window;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class EIST_IM implements EntryPoint {
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final ChatServiceAsync chatService = GWT.create(ChatService.class);
	private LogIn login;
	private ChatBar chatBar;
	private ChatList chatList;
	private User user;
	final VerticalPanel main = new VerticalPanel();
	final HorizontalPanel lr = new HorizontalPanel();
	private final Label errorLabel=new Label();
	final ScrollPanel messagePanel = new ScrollPanel();
	final FlexTable messagesTable = new FlexTable();
	Actualizer actualizer;
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		login = new LogIn(chatService, this);
		main.add(errorLabel);
		main.add(login.getBox());
		lr.add(main);
		
		RootPanel.get("dynamic").add(lr);
		

		// Focus the cursor on the name field when the app loads
		login.setFocus(true);
	}
	public void setErrorLabelText(String s){
		errorLabel.setText(s);
	}
	public User getUser(){
		return user;
	}
	public void setUser(User u){
		user = u;
	}
	public void setLoggedInUsers(List<User> list){
		chatList.setLoggedInUsers(list);
	}
	public void setLoggedInUser(User u){
		chatList.setLoggedInUser(u);
	}
	public void actualize(){
		actualizer = new Actualizer(this);
		actualizer.run();
	}
	/**
	 * sets up the Graphical User Interface
	 * clears all Panels before setting up new user interface.
	 */
	public void setGUI(){
		/**
		 * Used to logout user when user closes window or reloads.
		 */
		Window.addCloseHandler(new CloseHandler<Window>(){

			@Override
			public void onClose(CloseEvent<Window> event) {
				chatList.logout();
				
			}
		});
		messagePanel.clear();
		messagesTable.removeAllRows();
		messagePanel.add(messagesTable);
		
		// for view of current room and creating and changing rooms
		final Label roomLabel = new Label("Main Room");
		roomLabel.setStyleName("roomLabel");
		lr.clear();
		main.clear();
		main.add(roomLabel);
		main.add(messagePanel);
		messagesTable.setStyleName("messagesTable");
		chatBar = new ChatBar(chatService, this);
		main.add(chatBar.getHPanel());
		chatList = new ChatList(chatService, this);
		chatList.setUser(user);
		lr.add(main);
		lr.add(chatList.getVPanel());
	}
	/**
	 * clears lr-Panel and main-panel and invokes the onModuleLoadMethod.
	 * invoked when user has logged out.
	 */
	public void reload(){
		user = null;
		lr.clear();
		main.clear();
		actualizer.cancel();
		this.onModuleLoad();
	}
	/**
	 * Puts the messages in the flextable messagesTable.
	 * Adds User, timestamp and messagetext.
	 * @param messages
	 * @param timestamp
	 */
	public void setMessages(List<Message> messages, long timestamp){
		while(!messages.isEmpty()){
			int rows = messagesTable.getRowCount();
			Message m = messages.remove(0);
			long time = m.getTimestamp();
			Date d = new Date(time);
				String html = "<div align=\"left\"> <b>"+m.getSender().getUsername()+ "</b> "
				+ " <em>" + DateTimeFormat.getMediumDateTimeFormat().format(d) + "</em></div>"
				+ "<div align=\"left\">" + m.getText() + "</div>";
				messagesTable.setHTML(rows, 0, html);
		}
	}
	public void getLoggedInUsers(){
		actualizer.getLoggedInUsers();
	}
	public void removeLoggedInUser(User u){
		chatList.remove(u);
	}
	/**
	 * invokes the getHist() - method in the Actualizer-class
	 * to set the ChatHistory recorded since the start of the server
	 */
	public void setHist(){
		actualizer.getHist();
	}
}
